/**
 * @Copyright 2012, Founder Mobile Media Technology（Chongqing） Co., Ltd L.P. All
 *rights reserved. The information contained herein is confidential
 *and proprietary to Founder Mobile Media Technology（Chongqing） Co.,
 *Ltd, and considered a trade secret as defined under civil and
 *criminal statutes. Founder Mobile Media Technology（Chongqing） Co.,
 *Ltd shall pursue its civil and criminal remedies in the event of
 *unauthorized use or misappropriation of its trade secrets. Use of
 *this information by anyone other than authorized employees of
 *Founder Mobile Media Technology（Chongqing） Co., Ltd is granted
 *only under a written non-disclosure agreement, expressly
 *prescribing the scope and manner of such use.
 */
package com.g2room.io.file.randomAccessFile;

import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 
 * 
 * @createTime: Jun 15, 2013 10:07:11 PM
 * @author: <a href="mailto:gaoxg@FOUNDER.COM.CN">Xuguo Gao</a>
 * @version: 0.1
 * @lastVersion: 0.1
 * @updateTime:
 * @updateAuthor: <a href="mailto:gaoxg@FOUNDER.COM.CN">Xuguo Gao</a>
 * @changesSum:
 * 
 */
public class LargeMappedFiles {
	static int length = 0x8000000; // 128 Mb
	private static String filePath = "D:/workspace/code/Java_Win7/Tests/src/com/g2room/file/data/12431.log";
    
	public static void main(String[] args) throws Exception {
		// 为了以可读可写的方式打开文件，这里使用RandomAccessFile来创建文件。
		RandomAccessFile faf = new RandomAccessFile(filePath, "rw");
		FileChannel fc = faf.getChannel();
		// 注意，文件通道的可读可写要建立在文件流本身可读写的基础之上
		MappedByteBuffer out = fc.map(FileChannel.MapMode.READ_WRITE, 0, length);
		// 写128M的内容
		for (int i = 0; i < length; i++) {
			out.put((byte) 'x');
		}
		System.out.println("Finished writing");
		// 读取文件中间6个字节内容
		for (int i = length / 2; i < length / 2 + 6; i++) {
			System.out.print((char) out.get(i));
		}
		fc.close();
		faf.close();
	}
	
	
}
